﻿
Imports std

Namespace storageJobConsumerInternal
    Public Class mapslice
        Inherits memslice

        Private ReadOnly m As map(Of String, bytes) = Nothing
        Private _valuesize As Int64 = 0

        Public Sub New()
            m = New map(Of String, bytes)()
        End Sub

        Public Shared Shadows Function initial(ByVal name As String,
                                               ByVal config As configParser,
                                               ByVal c As mapslice) As Boolean
            Return memslice.initial(name, config, c)
        End Function

        Private Sub increment_valuesize(ByVal i As Int64)
            assert(_valuesize >= 0)
            _valuesize += i
        End Sub

        Private Sub decrement_valuesize(ByVal i As Int64)
            _valuesize -= i
            assert(_valuesize >= 0)
        End Sub

        Private Sub clear_valuesize()
            _valuesize = 0
        End Sub

        Protected Overrides Function delete(ByVal key As String) As Boolean
            Dim it As map(Of String, bytes).iterator = Nothing
            it = m.find(key)
            If it = m.end() Then
                Return False
            Else
                decrement_valuesize((+it).second.size())
                Return assert(m.erase(it))
            End If
        End Function

        Protected Overrides Function keycount() As Int64
            Return m.size()
        End Function

        Protected Overrides Function list() As vector(Of String)
            Dim rtn As vector(Of String) = Nothing
            rtn = New vector(Of String)()

            Dim it As map(Of String, bytes).iterator = Nothing
            it = m.begin()
            While it <> m.end()
                rtn.push_back((+it).first())
                it += 1
            End While
            Return rtn
        End Function

        Protected Overrides Function modify(ByVal key As String, ByVal buff As bytes) As Boolean
            Dim it As map(Of String, bytes).iterator = Nothing
            it = m.find(key)
            If it <> m.end() Then
                decrement_valuesize((+it).second.size())
            End If
            m(key) = buff
            increment_valuesize(buff.size())
            Return True
        End Function

        Protected Overrides Function read(ByVal key As String) As bytes
            Dim it As map(Of String, bytes).iterator = Nothing
            it = m.find(key)
            If it = m.end() Then
                Return Nothing
            Else
                Return (+it).second
            End If
        End Function

        Protected Overrides Function retire() As Boolean
            m.clear()
            clear_valuesize()
            Return True
        End Function

        Protected Overrides Function seek(ByVal key As String) As Boolean
            Return (m.find(key) <> m.end())
        End Function

        Protected Overrides Function sizeof(ByVal key As String) As Int64
            Dim it As map(Of String, bytes).iterator = Nothing
            it = m.find(key)
            If it = m.end() Then
                Return npos
            Else
                Return (+it).second.size()
            End If
        End Function

        Protected Overrides Function valuesize() As Int64
            Return _valuesize
        End Function

        Protected Overrides Function write(ByVal key As String, ByVal buff As bytes) As Boolean
            increment_valuesize(buff.size())
            m(key) += buff
            Return True
        End Function
    End Class
End Namespace
